import{
	initOssUserClient,
	checkOssUserClient
} from '../utils/frontupload';

const frontUploadFileMixin = {
	methods: {
		// 图片上传至 oss
		// opt：文件信息
		// path:oss文件路径
		// i：为了进度条设置的索引
		// that:为了进度条设置的指针
		// bucket:oss的bucket
		// type:上传的类型图片、音频、视频(默认图片)
		async frontFileUpload(opt, path, i, that, bucket = 'newxuejinguser', type = 1) {
			switch (type) {
				case 1: //上传的是图片
					if (opt.size >2* 1024 * 1024) {
						this.$dialog.alert({message:`请上传小于2MB的图片`});
						return;
					}
					// 获取文件后缀
					const tmp = opt.name.split('.');
					const extname = tmp.pop();
					const extList = ['jpg', 'jpeg', 'png', 'gif'];
					// 校验文件类型
					const isValid = extList.includes(extname);
					if (!isValid) {
						this.$dialog.alert({message:`只支持上传 jpg、jpeg、png、gif 格式的图片`});
						return;
					}
					break;
				case 2: //上传的是音频
				if (opt.size >50* 1024 * 1024) {
					this.$dialog.alert({message:`请上传小于50MB的音频`});
					 return;
					}
					// 获取文件后缀
					const tmpaudio = opt.name.split('.');
					const extaudioname = tmpaudio.pop();
					const extaudioList = ['mp3'];
					// 校验文件类型
					const isaudioValid = extaudioList.includes(extaudioname);
					if (!isaudioValid) {
						this.$dialog.alert({message:`只支持上传mp3格式的音频`});
						return;
					}
					break;
				case 3: //上传的是视频
					if (opt.size > 50 * 1024 * 1024) {
						this.$dialog.alert({message:`请上传小于50MB的视频`});
						return;
					}
					// 获取文件后缀
					const tmpvideo = opt.name.split('.');
					const extvideoname = tmpvideo.pop();
					const extvideoList = ['mp4'];
					// 校验文件类型
					const isvideoValid = extvideoList.includes(extvideoname);
					if (!isvideoValid) {
						this.$dialog.alert({message:`只支持上传mp4格式的视频`});
						return;
					}
					break;
				case 4://上传的是文件
				if (opt.size >50* 1024 * 1024) {
					this.$dialog.alert({message:`请上传小于50MB的文件`});
					 return;
					}
					// 获取文件后缀
					const tmpfiles = opt.name.split('.');
					const extfilesname = tmpfiles.pop();
					const extfilesList = ['txt','jpg','png','pdf','doc','xls','ppt','docx','xlsx','pptx','rar','zip'];
					// 校验文件类型
					const isfilesValid = extfilesList.includes(extfilesname);
					if (!isfilesValid) {
						this.$dialog.alert({message:`仅支持txt、jpg、png、pdf、doc、xls、ppt、docx、xlsx、pptx、rar、zip格式的文件`});
						return;
					}
				break;
			}
			// 检查是否已有 Oss Client
			if (bucket == "newxuejinguser") { //因为是公共读为了安全可以单独设置一个STS，保留STS
				var client = checkOssUserClient();
				
				if (client === null) {
					try {
						var res = await this.$frontaxios.get(this.$front.getStsUserToken);
						console.log(res,bucket)
						if (res.code == 1) {
							let oss = res.data;
							if (oss.statusCode == 200) {
								client = initOssUserClient(
									oss['accessKeyId'],
									oss['accessKeySecret'],
									oss['securityToken'],
									oss['expiration'],
									bucket
								);
							}
						} else {
							this.$toast.fail('请重新登录');
							this.$router.push({name:'my'});
						}
					} catch (fail) {
						this.$toast.fail(`${fail}`);
						return;
					}
				}
				//console.log(videoclient);return;
				try {
					console.log(await client.multipartUpload(path, opt,{}))
					const res1 = await client.multipartUpload(path, opt, {
						//progress: function(percentage) {
							// progress is generator
							// 上传进度条，el-upload 组件自带的方法
							//that.loadProgress[i] = Math.floor(percentage * 100);
						},
					//}
					);
					console.log(res1)
					if (res1.res.status === 200) {
						console.log(1111111)
						//that.progressFlag = false;
						// 去除 oss 分片上传后返回所带的查询参数，否则访问会 403
						const ossPath = res1.res.requestUrls[0].split('?')[0];
						// 替换协议，统一使用 'https://'，否则 Android 无法显示图片
						let url = ossPath.replace('http://', 'https://');
						this.$toast.success('上传成功');
						return url;
					}
				} catch (error) {
					console.log(error)
					this.$toast.fail(`${error}`);
				}
			} 
		}
	}
};

export default frontUploadFileMixin;
